﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using RMS.Controller;

namespace RMS.View
{
    public partial class AcademicDepartmentView : UserControl
    {
        private AcademicDepartmentController _controller;
        //private int _lastIndex;

        public AcademicDepartmentView()
        {
            InitializeComponent();
        }
        public AcademicDepartmentController Controller
        {
            get { return this._controller; }
            set { this._controller = value; }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            this._controller.HandleAddButtonClick();            
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            if (this.listViewHumanResources.SelectedItems.Count > 0)
            {
                this._controller.HandleRemoveButtonClick();
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            this._controller.HandleUpdateButtonClick();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            if (this.buttonSearch.Text == "Search")
            {
                this.buttonSearch.Text = "Cancel";
                this._controller.HandleSearchButtonClick();
            }
            else if (this.buttonSearch.Text == "Cancel")
            {
                this.buttonSearch.Text = "Search";
                this._controller.HandleSearchCancelButtonClick();
            }                
        }

        private void listViewHumanResources_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewHumanResources.SelectedItems.Count == 0)
            {
                //if (this.listViewHumanResources.Items.Count > 0)
                //{
                //    if (this._lastIndex > (this.listViewHumanResources.Items.Count - 1))
                //        this._lastIndex = this.listViewHumanResources.Items.Count - 1;
                //    this.listViewHumanResources.Focus();
                //    this.listViewHumanResources.Items[_lastIndex].Selected = true;                    
                //}
                return;
            }
            ListViewItem item = this.listViewHumanResources.SelectedItems[0];
            if(item == null) return;
            this.textBoxId.Text = item.SubItems[1].Text.ToString();
            this.textBoxUsername.Text = item.SubItems[2].Text.ToString();
            
            this.comboBoxAuthority.Text = item.SubItems[3].Text;            
            this.textBoxTitle.Text = item.SubItems[4].Text.ToString();
            this.textBoxFirstname.Text = item.SubItems[5].Text.ToString();
            this.textBoxLastname.Text = item.SubItems[6].Text.ToString();
            this.textBoxAddress.Text = item.SubItems[7].Text.ToString();
            this.textBoxPhonenumber.Text = item.SubItems[8].Text.ToString();
            //this._lastIndex = this.listViewHumanResources.SelectedIndices[0];
        }

        public void RefreshHumanResourcesList(List<Model.HumanResource> humanResources)
        {
            this.listViewHumanResources.Items.Clear();
            foreach (Model.HumanResource hr in humanResources)
            {
                ListViewItem item = new ListViewItem(hr.FirstName + " " + hr.LastName);

                item.SubItems.Add(hr.Id.ToString());
                item.SubItems.Add(hr.Username);
                switch (hr.Authority)
                {
                    case Model.UserType.Director:
                        item.SubItems.Add("Director");
                        break;
                    case Model.UserType.Teacher:
                        item.SubItems.Add("Teacher");
                        break;
                    case Model.UserType.Secretary:
                        item.SubItems.Add("Secretary");
                        break;
                    default:
                        item.SubItems.Add("Public Person");
                        break;
                }
                item.SubItems.Add(hr.Title);
                item.SubItems.Add(hr.FirstName);
                item.SubItems.Add(hr.LastName);
                item.SubItems.Add(hr.Address);
                item.SubItems.Add(hr.PhoneNumber);
                this.listViewHumanResources.Items.Add(item);
            }
        }

        public string TextBoxId
        {
            get { return this.textBoxId.Text; }
            set { this.textBoxId.Text = value; }
        }
        public string TextBoxUsername
        {
            get { return this.textBoxUsername.Text; }
            set { this.textBoxUsername.Text = value; }
        }
        public string TextBoxAuthority
        {
            get { return this.comboBoxAuthority.Text; }
            set { this.comboBoxAuthority.Text = value; }
        }
        public string TextBoxTitle
        {
            get { return this.textBoxTitle.Text; }
            set { this.textBoxTitle.Text = value; }
        }
        public string TextBoxFirstName
        {
            get { return this.textBoxFirstname.Text; }
            set { this.textBoxFirstname.Text = value; }
        }
        public string TextBoxLastName
        {
            get { return this.textBoxLastname.Text; }
            set { this.textBoxLastname.Text = value; }
        }
        public string TextBoxAddress
        {
            get { return this.textBoxAddress.Text; }
            set { this.textBoxAddress.Text = value; }
        }
        public string TextBoxPhoneNumber
        {
            get { return this.textBoxPhonenumber.Text; }
            set { this.textBoxPhonenumber.Text = value; }
        }
    }
}
